package com.ruoyi.storage.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.mapper.StoSalesDetailMapper;
import com.ruoyi.storage.service.IStoSalesDetailService;
import com.ruoyi.order.domain.SalesDetail;

/**
 * 订单出库Service业务层处理
 * 
 * @author 庞一恺
 * @date 2025-03-04
 */
@Service
public class StoSalesDetailServiceImpl implements IStoSalesDetailService
{
    @Autowired
    private StoSalesDetailMapper salesDetailMapper;
    @Autowired
    private ItemDetailServiceImpl itemDetailServiceImpl;
    @Autowired
    private ExitRecordServiceImpl exitRecordServiceImpl;

    /**
     * 查询订单出库
     * 
     * @param orderId 订单出库主键
     * @return 订单出库
     */
    @Override
    public SalesDetail selectSalesDetailByOrderId(String orderId)
    {
        return salesDetailMapper.selectSalesDetailByOrderId(orderId);
    }

    /**
     * 查询订单出库列表
     * 
     * @param salesDetail 订单出库
     * @return 订单出库
     */
    @Override
    public List<SalesDetail> selectSalesDetailList(SalesDetail salesDetail)
    {
        //如果订单状态为4，则改为“待发货”
        List<SalesDetail> salesDetailList = salesDetailMapper.selectSalesDetailList(salesDetail);
        for (SalesDetail salesDetail1 : salesDetailList) {
            if (salesDetail1.getStatus().equals("4")){
                salesDetail1.setStatus("待发货");
            }
        }



        return salesDetailList;
    }

    /**
     * 新增订单出库
     * 
     * @param salesDetail 订单出库
     * @return 结果
     */
    @Override
    public int insertSalesDetail(SalesDetail salesDetail)
    {
        return salesDetailMapper.insertSalesDetail(salesDetail);
    }


    /**
     * 批量删除订单出库
     * 
     * @param orderIds 需要删除的订单出库主键
     * @return 结果
     */
    @Override
    public int deleteSalesDetailByOrderIds(String[] orderIds)
    {
        return salesDetailMapper.deleteSalesDetailByOrderIds(orderIds);
    }

    /**
     * 删除订单出库信息
     * 
     * @param orderId 订单出库主键
     * @return 结果
     */
    @Override
    public int deleteSalesDetailByOrderId(String orderId)
    {
        return salesDetailMapper.deleteSalesDetailByOrderId(orderId);
    }

    /**
     * 订单出库
     *
     * @param  productList 产品列表,orderId 订单
     * @return 结果
     */
    @Override
    public int salesExit(List<String> productList, String orderId) {
        //TODO 生成出库单
        if (exitRecordServiceImpl.salesExit(productList,orderId)==1) {
            //TODO 删除库存
            String[] productListArray = productList.toArray(new String[productList.size()]);
            return itemDetailServiceImpl.deleteItemDetailByItemIds(productListArray);
        }


        return 0;
    }


}
